﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zyg.Utility.Helper;
using Zyg.SSO.Core.Business;

namespace Zyg.SSO.Web.Admin.Role
{
    public partial class ModifyRole : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!base.Account.HavePermission(Permission.RoleManage))
                {
                    throw new Exception("你无权查看此页面");
                }
                bool parseSucceed;

                Guid id = Request.QueryString["id"].ToGuid(out parseSucceed);

                if (parseSucceed)
                {
                    BindData(id);
                }
                else
                {
                    throw new Exception("对不起，你所访问的页面不存在");
                }
            }
        }

        protected void BindData(Guid id)
        {
            Zyg.SSO.Core.Business.Role role = Zyg.SSO.Core.Business.Role.Load(id);

            if (role == null)
            {
                throw new Exception("对不起，你所访问的页面不存在");
            }

            RoleName.Text = role.Name;
            RoleRemark.Text = role.Remark;
            RoleDateCreated.Text = role.DateCreated.ToString("yyyy年MM月dd日 H点mm分");

            // BtnModifyRoleAccount.Attributes.Add("onclick", "window.location.href='RoleAccounts.aspx?RoleId=" + id.ToString() + "'");
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            bool parseSucceed;

            Guid id = Request.QueryString["id"].ToGuid(out parseSucceed);

            Zyg.SSO.Core.Business.Role role = Zyg.SSO.Core.Business.Role.Load(id);

            if (role != null)
            {
                role.Remark = RoleRemark.Text;
                role.Save();

                Zyg.SSO.Core.Business.Log log = new Zyg.SSO.Core.Business.Log();
                log.AccountId = base.Account.Id;
                log.ModuleId = Zyg.SSO.Core.Business.Module.Role.Id;
                log.Message = string.Format("管理员 {0} 修改了角色 {1}", base.Account.Email, role.Name);

                log.Save();

                WriteBackScript("alert('保存成功')");
            }

        }
    }
}
